home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP13.ZIP / PATRON / PATRON.CPP < prev    next >
C/C++ Source or Header  |  1993-06-27  |  17KB  |  714 lines

  1. /*
  2.  * PATRON.CPP
  3.  * Modifications for Chapter 13
  4.  *
  5.  * WinMain which is all we need for the basic application.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #define INITGUIDS
  18. #include "patron.h"
  19.  
  20. //CHAPTER13MOD
  21. //Count number of objects and number of locks.
  22. ULONG       g_cObj=0;
  23. ULONG       g_cLock=0;
  24.  
  25. //Make window handle global so other code can cause a shutdown
  26. HWND        g_hWnd=NULL;
  27.  
  28. //Indicate if the user has control
  29. BOOL        g_fUser=TRUE;
  30. //End CHAPTER13MOD
  31.  
  32.  
  33. /*
  34.  * WinMain
  35.  *
  36.  * Purpose:
  37.  *  Main entry point of application.   Should register the app class
  38.  *  if a previous instance has not done so and do any other one-time
  39.  *  initializations.
  40.  */
  41.  
  42. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  43.     , LPSTR pszCmdLine, int nCmdShow)
  44.     {
  45.     LPCPatronFrame  pFR;
  46.     FRAMEINIT       fi;
  47.     WPARAM          wRet;
  48.  
  49.    #ifndef WIN32
  50.     SetMessageQueue(96);
  51.    #endif
  52.  
  53.     //Attempt to allocate and initialize the application
  54.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  55.  
  56.     fi.idsMin=IDS_FRAMEMIN;
  57.     fi.idsMax=IDS_FRAMEMAX;
  58.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  59.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  60.     fi.idStatMenuMin=ID_MENUFILE;
  61.     fi.idStatMenuMax=ID_MENUHELP;
  62.     fi.iPosWindowMenu=WINDOW_MENU;
  63.     fi.cMenus=CMENUS;
  64.  
  65.     //If we can initialize pFR, start chugging messages
  66.     if (pFR->FInit(&fi))
  67.         wRet=pFR->MessageLoop();
  68.  
  69.     delete pFR;
  70.     return wRet;
  71.     }
  72.  
  73.  
  74.  
  75. //CHAPTER13MOD
  76. /*
  77.  * ObjectDestroyed
  78.  *
  79.  * Purpose:
  80.  *  Function for the Patron Document object to call when it gets destroyed.
  81.  *  We destroy the main window if the proper conditions are met for
  82.  *  shutdown.
  83.  *
  84.  * Parameters:
  85.  *  None
  86.  *
  87.  * Return Value:
  88.  *  None
  89.  */
  90.  
  91. void FAR PASCAL ObjectDestroyed(void)
  92.     {
  93.     g_cObj--;
  94.  
  95.     //No more objects, no locks, no user control, shut the app down.
  96.     if (0==g_cObj && 0==g_cLock && IsWindow(g_hWnd) && !g_fUser)
  97.         PostMessage(g_hWnd, WM_CLOSE, 0, 0L);
  98.  
  99.     return;
  100.     }
  101.  
  102. //End CHAPTER13MOD
  103.  
  104.  
  105.  
  106.  
  107. /*
  108.  * CPatronFrame::CPatronFrame
  109.  * CPatronFrame::~CPatronFrame
  110.  *
  111.  * Constructor Parameters:
  112.  *  hInst           HINSTANCE from WinMain
  113.  *  hInstPrev       HINSTANCE from WinMain
  114.  *  pszCmdLine      LPSTR from WinMain
  115.  *  nCmdShow        int from WInMain
  116.  */
  117.  
  118. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  119.     , LPSTR pszCmdLine, int nCmdShow)
  120.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  121.     {
  122.     //CHAPTER13MOD
  123.     char        szTemp[256];
  124.  
  125.     m_fInitialized=FALSE;
  126.  
  127.     m_fEmbedding=FALSE; //-Embedding on command line?
  128.     ParseCmdLine(m_pszCmdLine, &m_fEmbedding, szTemp);
  129.  
  130.     g_fUser=!m_fEmbedding;
  131.  
  132.     m_dwRegCO=0;
  133.     m_pIClassFactory=NULL;
  134.     //End CHAPTER13MOD
  135.  
  136.     return;
  137.     }
  138.  
  139.  
  140. CPatronFrame::~CPatronFrame(void)
  141.     {
  142.     //CHAPTER13MOD
  143.     //Opposite of CoRegisterClassObject, takes class factory ref to 1
  144.     if (0L!=m_dwRegCO)
  145.         CoRevokeClassObject(m_dwRegCO);
  146.  
  147.     //This should be the last ::Release, which frees the class factory.
  148.     if (NULL!=m_pIClassFactory)
  149.         m_pIClassFactory->Release();
  150.     //End CHAPTER13MOD
  151.  
  152.     OleFlushClipboard();
  153.  
  154.     if (m_fInitialized)
  155.         OleUninitialize();
  156.     return;
  157.     }
  158.  
  159.  
  160.  
  161.  
  162. /*
  163.  * CPatronFrame::FInit
  164.  *
  165.  * Purpose:
  166.  *  Call OleInitialize then calling down into the base class
  167.  *  initialization.
  168.  *
  169.  * Parameters:
  170.  *  pFI             LPFRAMEINIT containing initialization parameters.
  171.  *
  172.  * Return Value:
  173.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  174.  */
  175.  
  176. BOOL CPatronFrame::FInit(LPFRAMEINIT pFI)
  177.     {
  178.     DWORD       dwVer;
  179.  
  180.     dwVer=OleBuildVersion();
  181.  
  182.     if (rmm!=HIWORD(dwVer))
  183.         return FALSE;
  184.  
  185.     if (FAILED(OleInitialize(NULL)))
  186.         return FALSE;
  187.  
  188.     m_fInitialized=TRUE;
  189.  
  190.     //CHAPTER13MOD
  191.     if (m_fEmbedding)
  192.         {
  193.         HRESULT     hr;
  194.  
  195.         m_pIClassFactory=new CLinkClassFactory(this);
  196.  
  197.         if (NULL==m_pIClassFactory)
  198.             return FALSE;
  199.  
  200.         //Since we hold on to this, we should AddRef it.
  201.         m_pIClassFactory->AddRef();
  202.  
  203.         hr=CoRegisterClassObject(CLSID_PatronPages, (LPUNKNOWN)m_pIClassFactory
  204.             , CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &m_dwRegCO);
  205.  
  206.         if (FAILED(hr))
  207.             return FALSE;
  208.         }
  209.     //End CHAPTER13MOD
  210.  
  211.  
  212.     return CFrame::FInit(pFI);
  213.     }
  214.  
  215.  
  216.  
  217.  
  218.  
  219. /*
  220.  * CPatronFrame::CreateCClient
  221.  *
  222.  * Purpose:
  223.  *  Constructs a new client specific to the application.
  224.  *
  225.  * Parameters:
  226.  *  None
  227.  *
  228.  * Return Value:
  229.  *  LPCClient       Pointer to the new client object.
  230.  */
  231.  
  232. LPCClient CPatronFrame::CreateCClient(void)
  233.     {
  234.     return (LPCClient)(new CPatronClient(m_hInst));
  235.     }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242. /*
  243.  * CPatronFrame::FRegisterAllClasses
  244.  *
  245.  * Purpose:
  246.  *  Registers all classes used in this application.
  247.  *
  248.  * Parameters:
  249.  *  None
  250.  *
  251.  * Return Value:
  252.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  253.  */
  254.  
  255. BOOL CPatronFrame::FRegisterAllClasses(void)
  256.     {
  257.     WNDCLASS        wc;
  258.  
  259.     //First let the standard frame do its thing
  260.     if (!CFrame::FRegisterAllClasses())
  261.         return FALSE;
  262.  
  263.     //We need double-clicks now and for object activation later.
  264.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  265.     wc.hInstance     = m_hInst;
  266.     wc.cbClsExtra    = 0;
  267.     wc.lpfnWndProc   = PagesWndProc;
  268.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  269.     wc.hIcon         = NULL;
  270.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  271.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  272.     wc.lpszMenuName  = NULL;
  273.     wc.lpszClassName = SZCLASSPAGES;
  274.  
  275.     if (!RegisterClass(&wc))
  276.         return FALSE;
  277.  
  278.     return TRUE;
  279.     }
  280.  
  281.  
  282.  
  283. //CHAPTER13MOD
  284. /*
  285.  * CPatronFrame::FPreShowInit
  286.  *
  287.  * Purpose:
  288.  *  Called from FInit before intially showing the window.  We do whatever
  289.  *  else we want here, modifying m_nCmdShow as necessary which affects
  290.  *  ShowWindow in FInit.
  291.  *
  292.  * Parameters:
  293.  *  None
  294.  *
  295.  * Return Value:
  296.  *  BOOL            TRUE if this initialization succeeded, FALSE otherwise.
  297.  */
  298.  
  299. BOOL CPatronFrame::FPreShowInit(void)
  300.     {
  301.     //Base class does nothing
  302.     CFrame::FPreShowInit();
  303.  
  304.     //Save the window handle for shutdown if necessary.
  305.     g_hWnd=m_hWnd;
  306.  
  307.     //If we're -Embedding, don't show the window initially.
  308.     if (m_fEmbedding)
  309.         m_nCmdShow=SW_HIDE;
  310.  
  311.     return TRUE;
  312.     }
  313.  
  314.  
  315.  
  316. /*
  317.  * CPatronFrame::ParseCommandLine
  318.  *
  319.  * Purpose:
  320.  *  Allows the application to parse the command line and take action
  321.  *  after the window has possibly been shown.  For a compound document
  322.  *  server we need to just make sure that if -Embedding is there that
  323.  *  we take no file action.  FPreShowInit has already handled the
  324.  *  window visibility.
  325.  *
  326.  * Parameters:
  327.  *  None
  328.  *
  329.  * Return Value:
  330.  *  BOOL            TRUE if this initialization succeeded, FALSE otherwise.
  331.  */
  332.  
  333. void CPatronFrame::ParseCommandLine(void)
  334.     {
  335.     //If -Embedding was there, prevent any  attempt at loading a file.
  336.     if (m_fEmbedding)
  337.         return;
  338.  
  339.     CFrame::ParseCommandLine();
  340.     return;
  341.     }
  342. //End CHAPTER13MOD
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349. /*
  350.  * CPatronFrame::OnCommand
  351.  *
  352.  * Purpose:
  353.  *  WM_COMMAND handler for the Patron frame window that processes extra
  354.  *  File menu items as well as the Page menu.
  355.  *
  356.  * Parameters:
  357.  *  hWnd            HWND of the frame window.
  358.  *  wParam          WPARAM of the message.
  359.  *  lParam          LPARAM of the message.
  360.  *
  361.  * Return Value:
  362.  *  LRESULT         Return value for the message.
  363.  */
  364.  
  365. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  366.     {
  367.     LPCPatronDoc    pDoc;
  368.  
  369.     COMMANDPARAMS(wID, wCode, hWndMsg);
  370.  
  371.     /*
  372.      * Don't bother with anything during first initialization,
  373.      * skipping many GizmoBar notifications.
  374.      */
  375.     if (m_fI